#include<iostream>
#include<pthread.h>
#include<string>
#include <unistd.h>

void* threadrun(void *args)
{
    int cnt=10;
    while (cnt)
    {
        std::cout<<"new thread run ... ,cnt :"<<cnt--<<std::endl;
        sleep(1);
    }

    return nullptr;
    
}

std::string Printtid(pthread_t &tid)
{
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

int main()
{
    pthread_t tid;

    //传参
    int n=pthread_create(&tid,nullptr,threadrun,(void*)"thread 1");
    if(n!=0)
    {
        std::cerr<<"create thread error"<<std::endl;
        return 1;
    }

    //tid是什么样呢？其实是虚拟地址
    std::string tid_str=Printtid(tid);
    std::cout<<"tid:"<<tid_str<<std::endl;

    std::cout<<"main thread join"<<std::endl;
    n=pthread_join(tid,nullptr);    //不join，会造成类似僵尸进程的问题（主线程还活着一直不退）
    if(n==0)
    {
        std::cout<<"main thread wait success"<<std::endl;
    }
    return 0;
}
